package com.acwing.partition12;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/12/28 15:27
 */
public class AC1107魔板 {

    private static final char[][] graph = new char[2][4];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String start = "12345678";
        String[] s = reader.readLine().split("\\s+");
        StringBuilder end = new StringBuilder();
        for (int i = 0; i < 8; i++) end.append(s[i]);
        resolve(start, end.toString());
        writer.flush();
    }

    private static void resolve(String start, String end) throws IOException {
        if (start.equals(end)) {
            writer.write("0\n");
            return;
        }
        Map<String, Integer> dist = new HashMap<>();
        //prev记录当前状态是由哪个操作和哪个状态转移过来的
        Map<String, String[]> prev = new HashMap<>();
        LinkedList<String> queue = new LinkedList<>();
        queue.addLast(start);
        dist.put(start, 0);
        while (!queue.isEmpty()) {
            String u = queue.pollFirst();
            int dis = dist.get(u);
            //通过三种操作找到由当前节点能够达到的邻节点
            String[] next = {exchange(u), insertToLeftCol(u), rotate(u)};
            for (int i = 0; i < 3; i++) {
                String v = next[i];
                //当前状态已经搜索过就跳过，否则记录距离以及是由哪个状态通过什么操作转移过来的
                if (dist.containsKey(v)) continue;
                dist.put(v, dis + 1);
                prev.put(v, new String[]{String.valueOf((char) (i + 'A')), u});
                if (end.equals(v)) break;
                queue.addLast(v);
            }
        }
        writer.write(dist.get(end) + "\n");
        StringBuilder sb = new StringBuilder();
        while (!end.equals(start)) {
            String[] pre = prev.get(end);
            sb.append(pre[0]);
            end = pre[1];
        }
        sb.reverse();
        writer.write(sb.toString());
    }

    private static void setToBoard(String state) {
        for (int i = 0; i < 4; i++) graph[0][i] = state.charAt(i);
        for (int i = 3, j = 4; i >= 0; i--, j++) graph[1][i] = state.charAt(j);
    }

    private static String getFromBoard() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) sb.append(graph[0][i]);
        for (int j = 3; j >= 0; j--) sb.append(graph[1][j]);
        return sb.toString();
    }

    private static String exchange(String state) {
        setToBoard(state);
        for (int i = 0; i < 4; i++) {
            char ch = graph[0][i];
            graph[0][i] = graph[1][i];
            graph[1][i] = ch;
        }
        return getFromBoard();
    }

    private static String insertToLeftCol(String state) {
        setToBoard(state);
        char t1 = graph[0][3], t2 = graph[1][3];
        for (int j = 3; j > 0; j--) {
            for (int i = 0; i < 2; i++) {
                graph[i][j] = graph[i][j - 1];
            }
        }
        graph[0][0] = t1;
        graph[1][0] = t2;
        return getFromBoard();
    }

    private static String rotate(String state) {
        setToBoard(state);
        char t = graph[0][1];
        graph[0][1] = graph[1][1];
        graph[1][1] = graph[1][2];
        graph[1][2] = graph[0][2];
        graph[0][2] = t;
        return getFromBoard();
    }
}
